<template>
  <view class="plate-filter">
    <view class="scroll-container">
      <view class="plate-list" v-if="plates">
        <view 
          class="plate-item"
          :class="{ active: currentValue === '' }"
          @click="handleSelect('')"
        >
          全部
        </view>
        <view 
          class="plate-item"
          :class="{ active: currentValue === plate.plateId.toString() }"
          @click="handleSelect(plate.plateId.toString())"
          v-for="plate in plates" 
          :key="plate.plateId"
        >
          {{ plate.plateName }}
        </view>
      </view>
    </view>
  </view>
</template>

<script setup>
import { watch, ref } from 'vue';

const props = defineProps({
  plates: {
    type: Array,
    required: false,
    default: () => []
  },
  modelValue: {
    type: String,
    default: ''
  }
});

const emit = defineEmits(['update:modelValue', 'change']);

// 内部响应式变量，同步props.modelValue
const currentValue = ref(props.modelValue);

// 监听props.modelValue变化，同步到内部变量
watch(() => props.modelValue, (newVal) => {
  currentValue.value = newVal;
});

// 选择处理函数 - 只在用户点击时触发change事件
const handleSelect = (val) => {
  // 只有当值发生变化时才触发事件
  if (currentValue.value !== val) {
    currentValue.value = val;
    emit('update:modelValue', val); // 更新v-model绑定的值
    emit('change', val); // 通知父组件选中了哪个板块
  }
};
</script>

<style lang="scss" scoped>
@import './styles/plate-filter.scss';
</style>
